<?php

namespace App\Console\Commands;

use App\Services\SmsService;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class NotifyReviewersReviewMiddle extends Command
{
    protected $signature = 'notify-reviewers-review-middle {mode=check}';

    const MODE_CHECK = 'check';
    const MODE_TEST = 'test';
    const MODE_SEND = 'send';

    /**
     * @var 脚本运行模式
     *
     * check: 拉数据看情况 只输出到控制台 不发送短信
     * test: 仅发送一条测试短信给开发人员
     * send: 正式发送给用户
     */
    private $mode; // MODE_CHECK | MODE_TEST | MODE_SEND

    private $testDeveloperTel = '13037679713';

    public function handle()
    {
        $this->mode = $this->argument('mode');
        $this->log("mode: {$this->mode}");
        if (!in_array($this->mode, [self::MODE_CHECK, self::MODE_TEST, self::MODE_SEND])) {
            $this->log("Bad mode: {$this->mode}");
            return;
        }

        $smsSvc = new SmsService();
        $templateCode = config('sms.template_codes.评审中期通知');
        $this->log("templateCode: $templateCode");
        $rows = DB::select(
            'select reviewer_id, count(project_id) as project_count from reviewer_score_projects where is_submitted = 0 group by reviewer_id order by reviewer_id'
        );
        $rowCount = count($rows);
        foreach ($rows as $i => $row) {
            $rowNum = $i + 1;
            $reviewer = User::find($row->reviewer_id);
            if (!empty($reviewer->review_middle_sms_notified_at)) {
                $this->log("$rowNum/$rowCount Reviewer #{$reviewer->id} {$reviewer->name} already notified");
                continue;
            }
            $this->log("$rowNum/$rowCount Notify reviewer #{$reviewer->id}");
            $tel = $reviewer->tel;
            if ($this->mode === self::MODE_TEST) {
                $tel = $this->testDeveloperTel;
                $this->log("Running in TEST mode, change reviewer tel to test developer tel {$this->testDeveloperTel}");
            }
            $templateParamJson = json_encode([
                'name' => $reviewer->name,
                'projects' => $row->project_count,
                'username' => $reviewer->username,
                'password' => $reviewer->password,
            ], JSON_UNESCAPED_UNICODE);
            $this->log("Sms reviewer: {$reviewer->name}, tel: $tel, vars: $templateParamJson");
            if ($this->mode === self::MODE_CHECK) {
                $this->log("Running in CHECK mode, do not send sms");
            } elseif ($this->mode === self::MODE_TEST) {
                $this->log("Sending sms");
                $smsSvc->sendTemplateSms($tel, $templateCode, $templateParamJson);
                $this->log("Sms sent");
                $this->log("Running in TEST mode, send one sms only, return");
                return;
            } elseif ($this->mode === self::MODE_SEND) {
                $this->log("Sending sms");
                $smsSvc->sendTemplateSms($tel, $templateCode, $templateParamJson);
                $this->log("Sms sent");
                $this->log("Running in SEND mode, set reviewer review_middle_sms_notified_at = now()");
                $reviewer->update(['review_middle_sms_notified_at' => now()]);
            }
        }
    }

    private function log($msg)
    {
        $now = now();
        $line = "[$now]$msg";
        $this->line($line);
    }
}
